package com.coocaa.x.service;

import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.app.CoocaaService;
import com.coocaa.x.service.liteservice.LiteServiceManager;

import java.util.ArrayList;
import java.util.List;

public class LiteBaseService extends CoocaaService {
    private static List<String> activatedServices = new ArrayList<String>();

    @Override
    public void onCreate() {
        super.onCreate();
        LiteServiceManager.bind(getApplicationContext());

        synchronized (activatedServices) {
            if (!activatedServices.contains(getName()))
                activatedServices.add(getName());
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        CoocaaApplication.post(new Runnable() {
            @Override
            public void run() {
                LiteServiceManager.unbind(getApplicationContext());
            }
        });

        synchronized (activatedServices) {
            if (activatedServices.contains(getName()))
                activatedServices.remove(getName());
            if (activatedServices.size() == 0)
                android.os.Process.killProcess(android.os.Process.myPid());
        }
    }

}
